/*
 * Package il.ac.biu.cs.grossmm.api.data
 * File Value.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.api.data;

import java.util.WeakHashMap;

/**
 * Represents a data node type with a single property and with no node-sets.
 * This node type is defined only by a type of values which may be contained in
 * the only property in nodes of this type. Two instances of this class are
 * equal if they are defined using the same property value class.
 * 
 * @param <V>
 *            type of value which can be stored in the only property of nodes of
 *            this node type
 */
public final class NodeOf<V> implements NodeType<NodeOf<V>> {

	/**
	 * Returns an instance of this class defined by a given node type
	 * 
	 * @param <V>
	 *            type of value which can be stored in the only property of
	 *            nodes of this node type
	 * @param valueClass
	 *            Class object of value which can be stored in the only property
	 *            of nodes of this node type
	 * @return an instance of this class defined by a given node type
	 */
	@SuppressWarnings("unchecked")
	// static context...
	public static <V> NodeOf<V> nodeOf(Class<V> valueClass)
	{
		NodeOf<V> container = cache.get(valueClass);
		
		if (container == null) {
			container = new NodeOf<V>(valueClass);
			cache.put(valueClass, container);
		}

		return container;
	}

	/**
	 * Returns the only property for a simple node type based on a given java
	 * type
	 * 
	 * @param <V>
	 *            java type
	 * @param vClass
	 *            java Class object for this type
	 * @return the only property for a simple node type based on a given java
	 *         type
	 */
	public static <V> Property<NodeOf<V>, V> property(Class<V> vClass)
	{
		return nodeOf(vClass).PROPERTY;
	}

	/**
	 * The only property of corresponding node type
	 */
	public final Property<NodeOf<V>, V> PROPERTY;

	static WeakHashMap<Class, NodeOf> cache =
		new WeakHashMap<Class, NodeOf>();

	private Class<V> valueClass;

	private static class Prop<V> implements Property<NodeOf<V>, V> {
		NodeOf<V> parent;

		public Prop(NodeOf<V> parent)
		{
			this.parent = parent;
		}

		public int index()
		{
			return 0;
		}

		public Class<V> getValueClass()
		{
			return parent.valueClass;
		}

		public boolean isUnique()
		{
			return false;
		}

		public NodeOf<V> getParentNodeType()
		{
			return parent;
		}

	}

	private NodeOf(Class<V> valueClass)
	{
		this.PROPERTY = new Prop<V>(this);
		this.valueClass = valueClass;
	}

	public int nodesetsNumber()
	{
		return 0;
	}

	public int propertiesNumber()
	{
		return 1;
	}

	public Nodeset nodeset(int index)
	{
		throw new IndexOutOfBoundsException();
	}

	public Property property(int index)
	{
		if (index == 0)
			return PROPERTY;
		throw new IndexOutOfBoundsException();
	}

	@Override
	public boolean equals(Object obj)
	{
		if (obj == null || !(obj instanceof NodeOf))
			return false;

		NodeOf v = (NodeOf) obj;

		return v.valueClass.equals(valueClass);
	}

	@Override
	public int hashCode()
	{
		return valueClass.hashCode() ^ 0xdeed00f;
	}

	@Override
	public String toString()
	{
		return "NodeOf "+valueClass.getSimpleName();
	}
}
